package com.example.taobaounion.ui.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.taobaounion.R
import com.example.taobaounion.model.bean.SelectCategoryContentBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean
import com.example.taobaounion.ui.fragment.SelectFragment

/**
 *@author : Administrator
 *@descreption : 精选页面的分类recyclerview的适配器
 */
class SelectCategoryContentAdapter : RecyclerView.Adapter<SelectCategoryContentAdapter.ViewHolder>() {
    private lateinit var mContext: Context
    private var mDataList= ArrayList<UatmTbkItemBean>()
    var mSelectCategoryContentItemOnClickListener: SelectCategoryContentItemOnClickListener? = null
    var prePosition = 0

    fun setData(dataList: List<UatmTbkItemBean>) {
        this.mDataList.clear()
        this.mDataList.addAll(dataList)
        notifyDataSetChanged()
    }

    fun setSelectCategoryItemOnClickListener(selectCategoryContentItemOnClickListener: SelectCategoryContentItemOnClickListener) {
        this.mSelectCategoryContentItemOnClickListener = selectCategoryContentItemOnClickListener
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val goodsTitle: TextView = itemView.findViewById(R.id.goodsTitle)
        val goodsPic: ImageView = itemView.findViewById(R.id.goodsPic)
        val couponInfo: TextView = itemView.findViewById(R.id.couponInfo)
        val finalPrice: TextView = itemView.findViewById(R.id.finalPrice)
        val noTicketTv: TextView = itemView.findViewById(R.id.noTicketTv)
        val itemRoot: CardView = itemView.findViewById(R.id.itemRoot)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        mContext = parent.context
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_select_category_content, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return this.mDataList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Glide.with(mContext).load(mDataList[position].pict_url).into(holder.goodsPic)
        holder.goodsTitle.text = mDataList[position].title
        if (mDataList[position].coupon_info!=null){
            holder.couponInfo.text =  mDataList[position].coupon_info
            holder.finalPrice.text = String.format(mContext.resources.getString(R.string.text_select_final_price), mDataList[position].zk_final_price)
        }else{
            holder.couponInfo.visibility = View.GONE
            holder.finalPrice.visibility = View.GONE
            holder.noTicketTv.visibility = View.VISIBLE

        }

        holder.itemRoot.setOnClickListener {
            mSelectCategoryContentItemOnClickListener?.contentItemOnClick(mDataList[position])
        }



    }

    interface SelectCategoryContentItemOnClickListener {
        fun contentItemOnClick(dataBean: UatmTbkItemBean)
    }
}